package com.tzbank.fingerPrint.esb.provide;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.springframework.util.FastByteArrayOutputStream;

import jodd.io.StreamUtil;



public class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper {

	private  byte[] body;

	public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
		super(request);
		System.out.println("字符"+request.getCharacterEncoding());
		request.setCharacterEncoding("utf-8");
		//BufferedReader BufferedReader=request.getReader();
		FastByteArrayOutputStream output= new FastByteArrayOutputStream();
/*
		 @SuppressWarnings("resource")
		Writer out = new OutputStreamWriter(output, "UTF-8");
		 
		 
		    char[] buffer = new char[32768];
		    int count = 0;
		    int read =0;
		    
		    while ((read = request.getReader().read()) != -1) {
		      
		    	out.write(buffer, 0, read);
		    	count += read;
		    }
		    System.out.println(">>>>>>>."+count);*/
			body = readBytes(request.getReader(),"UTF-8");
		
	}

	
	
	
	 public static void close(OutputStream out)
	  {
	    if (out != null) {
	      try {
	        out.flush();
	      }
	      catch (IOException ioex) {
	      }
	      try {
	        out.close();
	      }
	      catch (IOException ioe)
	      {
	      }
	    }
	  }

	  public static void close(Reader in)
	  {
	    if (in != null)
	      try {
	        in.close();
	      }
	      catch (IOException ioe)
	      {
	      }
	  }

	  public static void close(Writer out)
	  {
	    if (out != null) {
	      try {
	        out.flush();
	      }
	      catch (IOException ioex) {
	      }
	      try {
	        out.close();
	      }
	      catch (IOException ioe)
	      {
	      }
	    }
	  }




	  




	  public static byte[] readBytes(Reader input, String encoding) throws IOException {
	    FastByteArrayOutputStream output = new FastByteArrayOutputStream();
	    copy(input, output, encoding);
	    return output.toByteArray(); }


	  public static void copy(Reader input, OutputStream output, String encoding)
			    throws IOException
			  {
			    Writer out = new OutputStreamWriter(output, encoding);
			    copy(input, out);
			    out.flush();
			  }

	  public static int copy(Reader input, Writer output)
			    throws IOException
			  {
			    char[] buffer = new char[32768];
			    int count = 0;
			    int read;
			    
			    while ((read = input.read(buffer, 0, 32768)) >= 0) {
			      
			      output.write(buffer, 0, read);
			      count += read;
			    }
			    
			    output.flush();
			    return count;
			  }
	
	
	
	
	
	
	
	
	
	
	
	
	@Override
	public BufferedReader getReader() throws IOException {
		return new BufferedReader(new InputStreamReader(getInputStream()));
	}

	@Override
	public ServletInputStream getInputStream() throws IOException {
		final ByteArrayInputStream bais = new ByteArrayInputStream(body);
		return new ServletInputStream() {
			@Override
			public int read() throws IOException {
				return bais.read();
			}

			@Override
			public boolean isFinished() {
				// TODO 自动生成的方法存根
				return false;
			}

			@Override
			public boolean isReady() {
				// TODO 自动生成的方法存根
				return false;
			}

			@Override
			public void setReadListener(ReadListener listener) {
				// TODO 自动生成的方法存根

			}
		};
	}

}
